package com.neko.seed.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author monik
 */
public class ExcelUtils {
    public static BigDecimal excelDecimalRow(Row excelRow, int index){
        Cell cell = excelRow.getCell(index);
        if (null == cell){
            return new BigDecimal(0);
        }else {
            int cellType = cell.getCellType();
            if (cellType == Cell.CELL_TYPE_NUMERIC){
                return BigDecimal.valueOf(cell.getNumericCellValue());
            }else if (cellType == Cell.CELL_TYPE_STRING){
                try {
                    return new BigDecimal(cell.getStringCellValue());
                }catch (Exception e){
                    return BigDecimal.ZERO;
                }
            }else if (cellType == Cell.CELL_TYPE_FORMULA){
                return BigDecimal.valueOf(cell.getNumericCellValue());
            }else {
                return BigDecimal.ZERO;
            }
        }
    }

    public static String excelRowStringVal(Row excelRow, int index){
        if (null == excelRow.getCell(index)){
            return "";
        }else {
            int cellType = excelRow.getCell(index).getCellType();
            if (cellType == Cell.CELL_TYPE_STRING){
                return excelRow.getCell(index).getStringCellValue();
            }else if(cellType == Cell.CELL_TYPE_NUMERIC){
                BigDecimal numberVal =  BigDecimal.valueOf(excelRow.getCell(index).getNumericCellValue());
                return numberVal.toPlainString();
            }else {
                return "";
            }
        }
    }

    public static Date excelRowDateVal(Row excelRow, int index){
        if (null == excelRow.getCell(index)){
            return null;
        }else {
            try {
                String dateStr = excelRow.getCell(index).getStringCellValue();
                return DateUtils.parseDate(dateStr);
            }catch (Exception e){
                Date date = excelRow.getCell(index).getDateCellValue();
                return date;
            }
        }
    }

    /**判断文件格式
     * @Author monik
     * @Description
     * @Date 10:07 下午 2020/12/26
     * @param inStr
     * @param fileName
     * return org.apache.poi.ss.usermodel.Workbook
     */
    public static Workbook getWorkbook(InputStream inStr, String fileName) throws Exception {
        Workbook workbook = null;
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        if (".xls".equals(fileType)) {
            workbook = new HSSFWorkbook(inStr);
        } else if (".xlsx".equals(fileType)) {
            workbook = new XSSFWorkbook(inStr);
        } else {
            throw new Exception("请上传excel文件！");
        }
        return workbook;
    }

    public static Object getValue(Row row, int index){
        int cellType = row.getCell(index).getCellType();
        Object result;
        switch (cellType){
            case Cell.CELL_TYPE_NUMERIC:
                result = row.getCell(index).getNumericCellValue();
                break;
            case Cell.CELL_TYPE_FORMULA:
                result = row.getCell(index).getCellFormula();
                break;
            case Cell.CELL_TYPE_BLANK:
                result = "";
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                result = row.getCell(index).getBooleanCellValue();
                break;
            case Cell.CELL_TYPE_ERROR:
                result = row.getCell(index).getErrorCellValue();
                break;
            default:
                result = row.getCell(index).getStringCellValue();
                break;
        }
        return result;
    }
}
